#include "GeometryUtils.h"

using namespace cocos2d;

b2Vec2 GeometryUtils::convertCCPointToB2Vec2(CCPoint ccPoint)
{
	return b2Vec2(ccPoint.x / PhysicsConstants::WORLD_SCALE, ccPoint.y / PhysicsConstants::WORLD_SCALE);
}

CCPoint GeometryUtils::convertB2Vec2ToCCPoint(b2Vec2 point)
{
	return CCPoint(point.x * PhysicsConstants::WORLD_SCALE, point.y * PhysicsConstants::WORLD_SCALE);
}

float GeometryUtils::determinant(b2Vec2 u, b2Vec2 v, b2Vec2 w)
{
	return u.x * v.y + v.x * w.y +  w.x * u.y - u.x * w.y - v.x * u.y - w.x * v.y;
}

bool GeometryUtils::hasOneEdgeShared(b2PolygonShape* shape1, b2PolygonShape* shape2)
{
	int numberOfVertexShared = 0;
	/*CCLog("shape1 vertexCount:%i shape2 vertexCount: %i", shape1->GetVertexCount(), shape2->GetVertexCount());*/
	for (int i = 0; i < shape1->GetVertexCount(); i++)
	{
		b2Vec2 vertex1 = shape1->GetVertex(i);
		for (int j = 0; j < shape2->GetVertexCount(); j++)
		{
			b2Vec2 vertex2 = shape2->GetVertex(j);
			/*CCLog("vertex1X:%f vertex2X:%f vertex1Y:%f vertex2Y:%f", vertex1.x, vertex2.x, vertex1.y, vertex2.y);*/
			if ((vertex1.x - vertex2.x) < 1E-3 
				&& (vertex1.y == vertex2.y) < 1E-3)
				numberOfVertexShared++;
			if (numberOfVertexShared > 1)
				return true;
		}
	}
	return false;
}